package com.kaver.sso.helper;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kaver.sso.config.PropertiesConfig;
import com.kaver.sso.def.resp.User;

public class SsoHelper {
	@Autowired
	PropertiesConfig propertiesConfig;
	
	/**
     * 校验权限 
     * @return -1: 此权限未配置 0：无权限 1：有权限
     * */
    public Integer checkRight(String userId, String identifier) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", userId);
        jsonObject.put("groupId", propertiesConfig.getGroupId());
        jsonObject.put("identifier", identifier);
        try {
            String result = HttpUtil.doPost(jsonObject.toJSONString(), null, propertiesConfig.getSsoServerDomain() + "/api/rights/hasRights");
            JSONObject object = JSON.parseObject(result);
            if (object.getBoolean("success")) {
                return object.getInteger("data");
            }
        } catch (IOException e) {
            
        }
        return 0;
    }
    
    /**
     * 校验sessionId
     * */
    public User checkSessionId(String sessionId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", sessionId);
        try {
            String result = HttpUtil.doPost(jsonObject.toJSONString(), null, propertiesConfig.getSsoServerDomain() + "/api/user/tokenInfo");
            JSONObject object = JSON.parseObject(result);
            if (object.getBoolean("success")) {
                return object.getJSONObject("data").toJavaObject(User.class);
            }
        } catch (IOException e) {
            
        }
        return null;
    }
}
